<template>
	<div class="addMold">
		<!-- 新增编辑 -->
		<Modal :mask-closable="false" v-model="create" :title="addtitle" class="modal-content-layout" width="80%">
			<div class="content-item">
				<Row>
					<Col class="pd16">
						<Form ref="form" :disabled="disabled" class="formList" :model="formItem" label-position="top" :rules="ruleValidate">
							<Row :gutter="20">
								<Col span="8">
									<FormItem label="工装名称" prop="equipName">
										<Input type="text" v-model="formItem.equipName" :placeholder="`${$t('common.inputInfo')}`" />
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="资产编号" prop="assetCode">
										<Input type="text" v-model="formItem.assetCode" :placeholder="`${$t('common.inputInfo')}`" />
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="工装编号" prop="equipCode">
										<Input type="text" :readonly="addtitle == '编辑'" v-model="formItem.equipCode" :placeholder="`${$t('common.inputInfo')}`" />
									</FormItem>
								</Col>
							</Row>
							<Row :gutter="20">
								<Col span="8">
									<FormItem label="工装分类名称" prop="classificationId">
										<Select
											filterable
											v-model="formItem.classificationId"
											:remote-method="remoteMethod"
											:loading="loading1"
											clearable
											@on-select='moldChange'
											:placeholder="`${$t('common.selectInfo')}`"
										>
											<Option v-for="item of moldList" :value="item.id" :key="item.id" :label="item.classificationName"></Option>
										</Select>
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="工装类型" prop="moldModel">
										<Input readonly type="text" v-model="formItem.moldModel" :placeholder="`${$t('common.inputInfo')}`" />
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="工装状态" prop="moldState">
										<Select v-model="formItem.moldState" clearable :max-tag-count="1" :placeholder="`${$t('common.selectInfo')}`">
											<Option v-for="item of moldStateList" :value="item.value" :key="item.value" :label="item.value"> </Option>
										</Select>
									</FormItem>
								</Col>
								<Col span="8"> </Col>
							</Row>
							<Row :gutter="20">
								<Col span="8">
									<FormItem label="工装寿命" prop="moldLife">
										<InputNumber
											type="number"
											:max="99999999"
											style="width: 100%"
											:step="1"
											:min="0"
											v-model="formItem.moldLife"
											:placeholder="`${$t('common.inputInfo')}`"
										>
										</InputNumber>
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="工装位置" prop="functionLocation">
										<Cascader clearable :data="orgList" v-model="formItem.functionLocation" :placeholder="`${$t('common.selectInfo')}`" change-on-select>
										</Cascader>
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="二级保养模次" prop="secondaryFrequency">
										<InputNumber
											type="number"
											:max="99999999"
											style="width: 100%"
											:min="0"
											onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
											v-model="formItem.secondaryFrequency"
											:placeholder="`${$t('common.inputInfo')}`"
										>
										</InputNumber>
									</FormItem>
								</Col>
							</Row>
							<Row :gutter="20">
								<Col span="8">
									<FormItem label="三级保养模次" prop="thirdLevelFrequency">
										<InputNumber
											type="number"
											:max="99999999"
											:min="0"
											style="width: 100%"
											onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
											v-model="formItem.thirdLevelFrequency"
											:placeholder="`${$t('common.inputInfo')}`"
										>
										</InputNumber>
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="评分" prop="score">
										<InputNumber
											type="number"
											onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
											:max="100"
											:min="0"
											style="width: 100%"
											v-model="formItem.score"
											:placeholder="`${$t('common.inputInfo')}`"
										>
										</InputNumber>
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="投用日期" prop="useDate">
										<DatePicker
											format="yyyy-MM-dd"
											:value="formItem.useDate"
											@on-change="
												(val) => {
													formItem.useDate = val;
												}
											"
											type="date"
											split-panels
											:placeholder="`${$t('common.selectInfo')}`"
										></DatePicker>
									</FormItem>
								</Col>
							</Row>

							<Row :gutter="20">
								<Col span="8">
									<FormItem label="验收日期" prop="acceptDate">
										<DatePicker
											format="yyyy-MM-dd"
											:value="formItem.acceptDate"
											@on-change="
												(val) => {
													formItem.acceptDate = val;
												}
											"
											type="date"
											split-panels
											:placeholder="`${$t('common.selectInfo')}`"
										></DatePicker>
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="制造年月" prop="manufactureDate">
										<DatePicker
											format="yyyy-MM-dd"
											:value="formItem.manufactureDate"
											@on-change="
												(val) => {
													formItem.manufactureDate = val;
												}
											"
											type="date"
											split-panels
											:placeholder="`${$t('common.selectInfo')}`"
										></DatePicker>
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="利用率(%)" prop="useRate">
										<InputNumber
											type="number"
											v-model="formItem.useRate"
											:precision="2"
											:max="100"
											style="width: 100%"
											:min="0"
											:active-change="false"
											:placeholder="`${$t('common.inputInfo')}`"
										>
											<span slot="append">%</span>
										</InputNumber>
									</FormItem>
								</Col>
							</Row>
							<Row :gutter="20">
								<Col span="8">
									<FormItem label="加工厂家" prop="processingManufacturer">
										<Input type="text" v-model="formItem.processingManufacturer" :placeholder="`${$t('common.inputInfo')}`" />
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="理论节拍(spm)" prop="moldingCycle">
										<Input
											type="number"
											v-model="formItem.moldingCycle"
											:min="0"
											@on-keyup="keyUp5($event, 'moldingCycle', true)"
											:max="999999"
											:placeholder="`${$t('common.inputInfo')}`"
										>
										</Input>
									</FormItem>
								</Col>
								<Col span="8">
									<FormItem label="是否启用" prop="isEnabled">
										<Select v-model="formItem.isEnabled" filterable clearable :placeholder="`${$t('common.selectInfo')}`">
											<Option :value="1" label="是"> </Option>
											<Option :value="0" label="否"> </Option>
										</Select>
									</FormItem>
								</Col>
							</Row>
							<Row :gutter="20">
								<Col span="24">
									<FormItem label="备注" prop="remarks">
										<Input
											type="textarea"
											:autosize="{ minRows: 1, maxRows: 3 }"
											v-model="formItem.remarks"
											:placeholder="`${$t('common.inputInfo')}`"
										/>
									</FormItem>
								</Col>
							</Row>
						</Form>
					</Col>
				</Row>
			</div>
			<div slot="footer">
				<Button type="default" @click="create = false">{{ $t('common.Cancle') }}</Button>
				<Button type="primary" v-if="!disabled" :loading="loading" @click="addOk('form')">{{ $t('maintenance.Confirmed') }}</Button>
			</div>
		</Modal>
	</div>
</template>
<script>
import { InputNumber } from 'view-design';
import { addApi, editApi } from '@/api/moldManagement/moldLedger.js';
import recursion from '@/util/recursion';
import { getDictData } from '@/api/index.js';

export default {
	name: 'addMold',
	components: { InputNumber },
	data() {
		const validateNumber = (rule, val, callback, message) => {
			if (val !== '') {
				return callback();
			}
			return callback(new Error(message));
		};
		return {
			orgList: [],
			create: false,
			disabled: false,
			formItem: {},
			addtitle: {},
			moldList: [],
			ruleValidate: {
				equipName: [{ required: true, message: this.$t('common.inputInfo') }],
				equipCode: [{ required: true, message: this.$t('common.inputInfo') }],
				moldModel: [{ required: true, message: this.$t('common.inputInfo') }],
				cavityNum: [{ required: true, message: this.$t('common.inputInfo') }],
				secondaryFrequency: [{ required: true, message: this.$t('common.inputInfo') }],
				thirdLevelFrequency: [{ required: true, message: this.$t('common.inputInfo') }],
				useRate: [{ required: true, message: '请输入利用率' }],
				moldingCycle: [{ required: true, validator: validateNumber, message: this.$t('common.inputInfo') }],
				score: [{ required: true, message: this.$t('common.inputInfo') }],
				moldState: [{ required: true, message: this.$t('common.selectInfo') }],
				isEnabled: [{ required: true, message: this.$t('common.selectInfo') }],
			},
			moldStateList: [],
		};
	},
	mounted() {
		this.getOrgList();
		this.getDictList();
	},
	methods: {
		moldChange(val){
			const arg=this.moldList.find(item=>item.id==val.value)
			this.$set(this.formItem,'moldModel',arg.equipAttributes)
			this.$set(this.formItem,'classificationName',arg.classificationName)
			this.$set(this.formItem,'classificationId',arg.id)
			console.log(arg,this.formItem);

		},
		getDictList() {
			getDictData('工装状态').then((res) => {
				this.moldStateList = res.data;
			});
		},
		getOrgList() {
			this.axios({
				method: 'get',
				url: '/basic/organization/findListByToken',
				params: { level: 0 },
			}).then((res) => {
				if (res.code === 200) {
					const obj = {
						label: 'name',
						value: 'code',
						children: 'children',
					};
					this.orgList = recursion.convertTree(res.data, obj);
				} else {
					this.$Message.warning({ content: res.msg, duration: 6, closable: true });
				}
			});
		},
		remoteMethod(query) {
			if (query !== '') {
				this.loading1 = true;
				setTimeout(() => {
					this.loading1 = false;
					this.axios({
						url:'/dms/classification/queryClassificationList',
						method: 'post',
						data: {
							pageSize:99,
							pageNum:1,
							classificationName: query,
						},
					}).then(res=>{
						this.moldList=res.data.records;
					}); //以输入的设备名去查询
				}, 200);
			} else {
				this.equipmentList = [];
			}
		},
		//增加或编辑
		createfaultCodeShowH(row) {
			this.disabled = false;
			this.$refs.form.resetFields();
			if (row.id) {
				this.addtitle = `${this.$t('common.Edit')}`;
				this.formItem = JSON.parse(JSON.stringify(row));
			} else {
				this.addtitle = `${this.$t('common.New')}`;
				this.formItem = {
					maintenanceMode: null,
					productionMode: null,
					moldLife: null,
					cavityNum: null,
					score: null,
					lengths: null,
					useRate:1.0,
					// thickness: null,
					moldingCycle: null,
					manufactureDate: null,
					secondaryFrequency: null,
					thirdLevelFrequency: null,
				};
			}
			this.create = true;
		},
		addOk(name) {
			this.$refs[name].validate((valid) => {
				if (!valid) {
					return;
				} else {
					this.loading = true;
					if (this.formItem.id) {
						editApi(this.formItem).then((res) => {
							this.loading = false;
							0;
							if (res.code === 200) {
								this.$Message.success('编辑成功');
								this.$emit('changeNum');
								this.create = false;
							} else {
								this.$Message.warning({
									content: res.msg,
									duration: 6,
									closable: true,
								});
							}
						});
					} else {
						addApi(this.formItem).then((res) => {
							this.loading = false;
							if (res.code === 200) {
								this.$Message.success('新增成功');
								this.$emit('changeNum');
								this.create = false;
							} else {
								this.$Message.warning({
									content: res.msg,
									duration: 6,
									closable: true,
								});
							}
						});
					}
				}
			});
		},
		check(row) {
			this.disabled = true;
			this.addtitle = `${this.$t('common.Check')}`;
			this.formItem = JSON.parse(JSON.stringify(row));
			this.create = true;
		},
		//获取仓库
		keyUp(e, key, money) {
			// 如果是删除为空 则不处理
			if (!this.formItem[key]) {
				return (this.formItem[key] = '');
			}
			// 每次抬键处理,对应金额渲染两位小数||数字
			let num = '';
			if (money) {
				num = this.formItem[key].match(/^\d*(\.?\d{0,2})/g)[0];
			} else {
				num = this.formItem[key].replace(/^[^\d]+$/g, '').split('.')[0];
			}
			this.formItem[key] = `${num}`;
		},
		keyUp5(e, key, money) {
			if (!this.formItem[key]) {
				return (this.formItem[key] = '');
			}
			let num = '';
			if (money) {
				num = this.formItem[key].match(/^\d*(\.?\d{0,5})/g)[0];
			} else {
				num = this.formItem[key].replace(/^[^\d]+$/g, '').split('.')[0];
			}
			this.formItem[key] = `${num}`;
		},
	},
};
</script>

<style scoped></style>
